<h1>Search</h1>
 
<form id="search_form" action="<?php echo url_for('page/search') ?>" class="search_form" method="POST">
  <?php echo $form['keywords'] ?>
  <input type="submit" value="Search" />
  <br />
  <?php echo $form['type'] ?>
  <?php echo $form['page'] ?>
</form>
<br />
<?php if (isset($authors) && count($authors)): ?>
<h3>Authors:</h3>
<div class="alternate_table">
  <table cellpadding="0" cellspacing="0" style="width: 100%">
    <tbody>
      <?php foreach($authors as $i => $author): ?>
        <tr class="<?php echo ($i%2==0)?'even':'odd' ?>">
          <td>
            <p>
              <?php echo link_to($author->getName(),'author_show', $author) ?>
            </p>
          </td>
          <td class="right_sided">
            <?php if($sf_user->isAuthenticated()): ?>
              <?php if($author->isBookmark($sf_user->getAttribute('user_id', null, 'sfGuardSecurityUser'))): ?>
                <a class="remove_book" title="Remove from bookmarks" href="<?php echo url_for('profile/removeAuthorBookmark?author_id='.$author->getId()) ?>"></a>
              <?php else: ?>
                <a class="add_book" title="Add to bookmarks" href="<?php echo url_for('profile/addAuthorBookmark?author_id='.$author->getId()) ?>"></a>
              <?php endif ?>
            <?php endif ?>
          </td>
        </tr>
      <?php endforeach ?>
    </tbody>
    <?php if($type && $type == SearchForm::AUTHORS): ?>
      <tfoot>
        <tr>
          <td class="bottom_row">
              <?php if ($authors->haveToPaginate()): ?>
                <div class="pagination">
                  <a href="#" page="1" class="search_paginate_link">
                    <img src="/images/first.png" alt="First page" title="First page" />
                  </a>
    
                  <a page="<?php echo $authors->getPreviousPage() ?>" class="search_paginate_link" href="#">
                    <img src="/images/previous.png" alt="Previous page" title="Previous page" />
                  </a>
    
                  <?php if($authors->getPage() > 3): ?> ... <?php endif ?>
    
                  <?php foreach ($authors->getLinks() as $page): ?>
                    <?php if ($page == $authors->getPage()): ?>
                      <?php echo $page ?>
                    <?php else: ?>
                      <a page="<?php echo $page ?>" class="search_paginate_link" href="#">
                        <?php echo $page ?>
                      </a>
                    <?php endif ?>
    
                  <?php endforeach ?>
    
                  <?php if($authors->getLastPage() - $authors->getPage() > 3): ?> ... <?php endif ?>
    
                  <a page="<?php echo $authors->getNextPage() ?>" class="search_paginate_link" href="#">
                    <img src="/images/next.png" alt="Next page" title="Next page" />
                  </a>
    
                  <a page="<?php echo $authors->getLastPage() ?>" class="search_paginate_link" href="#">
                    <img src="/images/last.png" alt="Last page" title="Last page" />
                  </a>
                </div>
              <?php endif; ?>
          </td>
        </tr>
        <tr>
          <td>
            <div class="pagination_desc">
              <?php if ($authors->haveToPaginate()): ?>
                Page <strong><?php echo $authors->getPage() ?> of <?php echo $authors->getLastPage() ?></strong>
              <?php endif; ?>
            </div>
          </td>
        </tr>
      </tfoot>
    <?php endif ?>
  </table>
</div>
<?php endif ?>


<?php if (isset($quotes) && count($quotes)): ?>
<h3>Quotes</h3>
<div class="alternate_table">
  <table cellpadding="0" cellspacing="0">
    <tbody>
      <?php foreach($quotes as $i => $quote): ?>
        <tr class="<?php echo ($i%2==0)?'even':'odd' ?>">
          <td>
            <p>
              <?php echo $quote->getContent() ?>
              <br />
              <?php echo link_to($quote->getAuthor()->getName(),'author_show', $quote->getAuthor()) ?>
            </p>
          </td>
          <td class="right_sided">
            <?php if($sf_user->isAuthenticated()): ?>
              <?php if($quote->isBookmark($sf_user->getAttribute('user_id', null, 'sfGuardSecurityUser'))): ?>
                <a class="remove_book" title="Remove from bookmarks" href="<?php echo url_for('profile/removeBookmark?quote_id='.$quote->getId()) ?>"></a>
              <?php else: ?>
                <a class="add_book" title="Add to bookmarks" href="<?php echo url_for('profile/addBookmark?quote_id='.$quote->getId()) ?>"></a>
              <?php endif ?>
            <?php endif ?>
          </td>
        </tr>
      <?php endforeach ?>
    </tbody>
    <?php if($type && $type == SearchForm::QUOTES): ?>
      <tfoot>
        <tr>
          <td class="bottom_row">
              <?php if ($quotes->haveToPaginate()): ?>
                <div class="pagination">
                  <a href="#" page="1" class="search_paginate_link">
                    <img src="/images/first.png" alt="First page" title="First page" />
                  </a>
    
                  <a page="<?php echo $quotes->getPreviousPage() ?>" class="search_paginate_link" href="#">
                    <img src="/images/previous.png" alt="Previous page" title="Previous page" />
                  </a>
    
                  <?php if($quotes->getPage() > 3): ?> ... <?php endif ?>
    
                  <?php foreach ($quotes->getLinks() as $page): ?>
                    <?php if ($page == $quotes->getPage()): ?>
                      <?php echo $page ?>
                    <?php else: ?>
                      <a page="<?php echo $page ?>" class="search_paginate_link" href="#">
                        <?php echo $page ?>
                      </a>
                    <?php endif ?>
    
                  <?php endforeach ?>
    
                  <?php if($quotes->getLastPage() - $quotes->getPage() > 3): ?> ... <?php endif ?>
    
                  <a page="<?php echo $quotes->getNextPage() ?>" class="search_paginate_link" href="#">
                    <img src="/images/next.png" alt="Next page" title="Next page" />
                  </a>
    
                  <a page="<?php echo $quotes->getLastPage() ?>" class="search_paginate_link" href="#">
                    <img src="/images/last.png" alt="Last page" title="Last page" />
                  </a>
                </div>
              <?php endif; ?>
          </td>
        </tr>
        <tr>
          <td>
            <div class="pagination_desc">
              <?php if ($quotes->haveToPaginate()): ?>
                Page <strong><?php echo $quotes->getPage() ?> of <?php echo $quotes->getLastPage() ?></strong>
              <?php endif; ?>
            </div>
          </td>
        </tr>
      </tfoot>
    <?php endif ?>
  </table>
</div>
<?php endif ?>

<script type="text/javascript">
  $(document).ready(function(){
    $('.search_form .autocomplete_box').autocomplete({ 
      serviceUrl:'<?php echo url_for('page/autocomplete') ?>',
      minChars:2, 
      delimiter: /(,|;)\s*/, // regex or character
      maxHeight:400,
      width:300,
      zIndex: 9999,
      deferRequestBy: 0, //miliseconds
      noCache: false, //default is false, set to true to disable caching
      // callback function:
      onSelect: function(value, data){ 
        //alert('You selected: ' + value + ' --> ' + data);
        //$('#search_author_id').val(data); 
      }
    });
    
    $('.search_paginate_link').click(function(event){
      event.preventDefault();
      $('#search_page').val($(this).attr('page'));
      $('#search_form').submit();
    })
  })
</script>

